/*
 * Cargo.cpp
 *
 *  Created on: Oct 8, 2011
 *      Author: lore
 */

#include "Cargo.h"

Cargo::Cargo() {
}

void Cargo::setCargo(string cargoCandidato){
	cargo=cargoCandidato;
}
string Cargo::getCargo(){
	return cargo;
}

Cargo::~Cargo() {
}

void Cargo::setCargoId(unsigned long cargoId) {
	id = cargoId;
}
unsigned long Cargo::getCargoId() {
	return id;
}

void Cargo::removeAllCargos(){
	idCargos.clear();
}

vector<unsigned long> Cargo:: getCargosSecundarios(){
	return idCargos;
}

void Cargo::addCargos(unsigned long idCargo){
	idCargos.push_back(idCargo);

}

void Cargo::SetCargosSecundariosId(vector<unsigned long> _idCargos){
		removeAllCargos();
		for (unsigned long i = 0; i < _idCargos.size(); i++) {
			idCargos.push_back(_idCargos.at(i));
		}
}

char* Cargo::pack() {
	char* buffer = new char[packLen()];

	char*ptr = reinterpret_cast<char*>(&id);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i] = *ptr;
		ptr++;
	}
	
	unsigned long tam = cargo.size();
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i+sizeof(unsigned long)] = *ptr;
		ptr++;
	}
	const char*str = cargo.c_str();
	for (unsigned long i = 0; i < cargo.size(); i++) {
		buffer[i+2*sizeof(unsigned long)] = str[i];
	}

	unsigned long packS = 2*sizeof(unsigned long)+cargo.size();

	tam = idCargos.size();
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		buffer[i+packS] = *ptr;
		ptr++;
	}	

	packS += sizeof(unsigned long);

	for (unsigned long i = 0; i < idCargos.size(); i++) {
		unsigned long cargoId = idCargos.at(i);
		ptr = reinterpret_cast<char*>(&cargoId);
		
		for (unsigned long j=0; j < sizeof (unsigned long); j++){
			//buffer[j+3*sizeof(unsigned long)+i*sizeof(unsigned long)+cargo.size()] = *ptr;
			buffer[packS+j+i*sizeof(unsigned long)] = *ptr;
			ptr++;
		}
	}
	return buffer;
}

bool Cargo::unpack(char* buffer, unsigned long bufferLen) {
	char*ptr = reinterpret_cast<char*>(&id);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i];
		ptr++;
	}

	unsigned long tam = cargo.size();
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr = buffer[i+sizeof(unsigned long)];
		ptr++;
	}
	stringstream ss;
	for (unsigned long i = 0; i < tam; i++) {
		ss << buffer[i+2*sizeof(unsigned long)];
	}
	cargo = ss.str();

	tam = idCargos.size();
	ptr = reinterpret_cast<char*>(&tam);
	for (unsigned long i = 0; i < sizeof(unsigned long); i++) {
		*ptr=buffer[i+2*sizeof(unsigned long)+cargo.size()];
		ptr++;
	}		

	unsigned long packS = 3*sizeof(unsigned long) +cargo.size();
	idCargos.clear();
	for (unsigned long i = 0; i < tam; i++) {
		unsigned long cargoId = 0;
		ptr = reinterpret_cast<char*>(&cargoId);
			
		for (unsigned long j=0; j < sizeof (unsigned long); j++){
			//*ptr = buffer[j+3*sizeof(unsigned long)+i*sizeof(unsigned long)+cargo.size()];
			*ptr = buffer[packS+j+i*sizeof(unsigned long)];
			ptr++;
		}
		idCargos.push_back(cargoId);
	}	
	return true;
}

unsigned long Cargo::packLen() {
	/*
	 * 	string cargo;
	unsigned long id;
	vector<unsigned long> idCargos;
	 */
	//unsigned long tam=  3*sizeof(unsigned long) + cargo.size()+ sizeof(unsigned long) * (idCargos.size());

	unsigned long tam = 0;
	tam += sizeof(unsigned long); // id
	tam += sizeof(unsigned long); // string size
	tam += sizeof(unsigned long); // vector size
	tam += cargo.size(); // cargo (TXT)
	tam += sizeof(unsigned long)*idCargos.size();


	return (tam);


}
